<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    //事件触发器
    class EventEmitter {
        constructor () {
            //{'click',:[fn1,fn2],'change':[fn]}
            this.subs = {}
        }
        //注册事件
        $on (eventType,handler) {
            this.subs[eventType] = this.subs[eventType] || []
            this.subs[eventType].push(handler )
        }
        //触发事件
        $emit (eventType,data) {
            if( this.subs[eventType]  ) {
                this.subs[eventType].forEach(handler => {
                    handler(data)
                });
            }
        }
    }
    let em = new EventEmitter()
    em.$on('click',(res)=>{  //先注册(订阅)click事件，再$emit触发这个事件
        console.log(res)
    })
    em.$on('click',(res)=>{  //先注册(订阅)click事件，再$emit触发这个事件
        console.log(222,res)
    })
    em.$emit('click','AAA')
    em.$emit('click','BBB')

</script>
</html>